<template>
  <div class="index-container">
    <div class="main-header">
      <el-row>
        <el-col :xs="8" :sm="8" :md="8" :lg="8" :xl="8">
          <el-button
            type="primary"
            icon="el-icon-plus"
            class="sureBtn"
            size="medium"
            @click="add"
          >
            添 加
          </el-button>
          <el-button
            type="success"
            icon="el-icon-search"
            class="sureBtn"
            size="medium"
            @click="edit(0)"
          >
            编辑/查看
          </el-button>
          <el-button
            type="danger"
            class="setBtn"
            icon="el-icon-delete"
            size="medium"
            @click="del"
          >
            删 除
          </el-button>
        </el-col>
        <el-col :xs="16" :sm="16" :md="16" :lg="16" :xl="16">
          <div class="search">
            <el-row :gutter="20">
              <el-col :xs="5" :sm="5" :md="5" :lg="5" :xl="5">
                <el-input
                  v-model="selectForm.name"
                  size="medium"
                  placeholder="老人姓名"
                  clearable
                ></el-input>
              </el-col>
              <el-col :xs="5" :sm="5" :md="5" :lg="5" :xl="5">
                <el-input
                  v-model="selectForm.mobile"
                  size="medium"
                  placeholder="联系方式"
                  clearable
                ></el-input>
              </el-col>
              <el-col :xs="4" :sm="4" :md="4" :lg="4" :xl="4">
                <el-input
                  v-model="selectForm.card"
                  size="medium"
                  placeholder="身份证"
                  clearable
                ></el-input>
              </el-col>
              <el-col :xs="5" :sm="5" :md="5" :lg="5" :xl="5">
                <el-select v-model="selectForm.type" clearable placeholder="请选择老人类别">
                    <el-option
                      v-for="(item, index) in options"
                      :key="item.index"
                      :label="item.label"
                      :value="item.label">
                    </el-option>
                  </el-select>
              </el-col>
              <el-col :xs="2" :sm="2" :md="2" :lg="2" :xl="2">
                <el-button
                  size="small"
                  type="primary"
                  icon="el-icon-search"
                  class="fr"
                  @click="search"
                >
                  搜索
                </el-button>
              </el-col>
            </el-row>
          </div>
        </el-col>
      </el-row>
    </div>
    <div class="main-content">
      <app-table
        ref="dataTable"
        :page-data="pageData"
        :table-columns="tableColumns"
        @current-change="handleCurrentChange"
        @handleCheckedRows="handleCheckedRows"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="success"
            @click="edit(scope.row)"
          >
            编辑
          </el-button>
        </template>
      </app-table>
    </div>
    <!-- 地图 -->
    <el-dialog
      title="选择地址"
      :visible.sync="mapVisible"
      width="60%"
      :before-close="closeMap"
      :close-on-click-modal="false"
      class="dialogForm"
    >
      <div class="dialog-main">
        <app-map
          :key="key2"
          ref="map"
          :keyword="keyword"
          @result="result"
        ></app-map>

      </div>
    </el-dialog>
    <!-- 地图end -->
    <!-- 添加老人start -->
    <el-dialog
      title="添加残疾人信息"
      :visible.sync="formVisible"
      width="60%"
      :before-close="closeForm"
      :close-on-click-modal="false"
      class="dialogForm"
    >
      <div class="dialog-main">
        <deformed-form
          :key="key2"
          ref="form"
          :datas="formData"
          @cancle="handleCancle"
          @handleMap="handleMap"
        ></deformed-form>
      </div>
    </el-dialog>
    <!-- 添加老人end -->
  </div>
</template>
<script>
  import appTable from "@/components/AppTable/index";
  import deformedForm from './components/deformedForm'
  import { getcanjielder } from "@/api/oldermanager";
  import appMap from "@/components/AppMap/index";
  export default {
    name: "Deformed",
    components: { appTable, appMap, deformedForm },
    filters: {
			valueFormat(value) {
				if (value == 0) {
					return '能力完好'
				} else if (value == 1) {
					return '轻度受损'
				} else if (value == 2) {
					return '中度受损'
				} else if (value >= 3) {
					return '重度受损'
				} else {
					return '评估不准确'
				}
			}
		},
    data() {
      return {
        //搜索数据
        selectForm: {},
        //列表数据
        pageData: {},
        //地图显隐
        mapVisible: false,
        //地图关键字
        keyword: "",
        key2: Math.random(),
        key3: Math.random(),

        //表格字段
        tableColumns: [
          { prop: "name", label: "姓名" },
          { prop: "sex", label: "性别", formatter: this.sexformatter },
          { prop: "age", label: "年龄" },
          { prop: "mobile", label: "联系方式" },
          {
            prop: "card",
            label: "身份证号",
            width: 200,
          },
          {
            prop: "address",
            label: "地址",
            width: 200,
          },
          { prop: "type", label: "残疾类别" }
        ],
        //表格选中列表
        currentSelectedData: [],
        // 老人类别
        options: [
          {
            label: "智力",
          },
          {
            label: "肢体",
          },
          {
            label: "精神",
          }
        ],
        //添加老人表单弹窗
        formVisible: false,
        base: {},
        formData: {
          base: {}
        }
      };
    },
    created() {},
    beforeDestroy() {},
    mounted() {
      this.getTableData();

    },
    methods: {
      //性别
      sexformatter(row) {
        if (row.sex == 1) {
          return "男";
        } else if (row.sex == 2) {
          return "女";
        }
      },
      //老人列表
      getTableData() {
        let data = this.selectForm;
        getcanjielder("get", data).then((resp) => {
          if (resp.code === 200) {
            this.pageData = resp.data;
            this.$set(this.pageData, "type", -1);
          }
        });
      },
      handleCurrentChange(val) {
        this.selectForm.page = val;
        this.getTableData();
      },
      search() {
        this.selectForm.page = 1;
        this.getTableData();
      },
      //勾选表格
      handleCheckedRows(rows) {
        if (rows && rows instanceof Array && rows.length > 0) {
          this.currentSelectedData = rows;
        } else {
          this.currentSelectedData = [];
        }
      },
      //关闭地图
      closeMap() {
        this.mapVisible = false;
      },
      //查找地图坐标
      handleMap(keyword) {
        this.key2 = Math.random();
        this.mapVisible = true;
        if (keyword) this.keyword = keyword;
      },
      //查找坐标地址返回
      result(e) {
        this.mapVisible = false;
        console.log(e)
        this.$set(this.base, "lng", e.lng);
        this.$set(this.base, "lat", e.lat);
        this.$set(this.base, "address", e.address);
        this.$set(this.formData, "base", this.base);
      },
      //添加残疾老人
      add() {
        this.key2 = Math.random();
        this.formData = {};
        this.formVisible = true;
      },
      //编辑
      edit(row) {
        this.key2 = Math.random();
        if(row) {
          this.formVisible = true;
          console.log(111,row)
          this.formData = Object.assign({}, row);
          return false
        }
        if (this.currentSelectedData.length === 0) {
          this.$message({
            message: "请选择一条您要查看的内容",
            type: "warning",
          });
        } else if (this.currentSelectedData.length > 1) {
          this.$message({
            message: "一次只能查看一条内容",
            type: "warning",
          });
        } else {
          this.formVisible = true;
          // this.e_id = this.currentSelectedData[0].id;
          this.formData = Object.assign({}, this.currentSelectedData[0]);
        }
      },
      //删除
      del() {
        if (this.currentSelectedData.length === 0) {
          this.$message({
            message: "请选择一条您要删除的内容",
            type: "warning",
          });
          return false
        } else if (this.currentSelectedData.length > 1) {
          this.$message({
            message: "一次只能删除一条内容",
            type: "warning",
          });
          return false
        }
        this.$confirm("确定删除该条数据, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            let param = {
              id: this.currentSelectedData[0].id,
            };
            getcanjielder("delete", param).then((resp) => {
              if (resp.code === 200) {
                this.$message({
                  type: "success",
                  message: "删除成功!",
                });
                this.getTableData();
              }
            });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "取消操作",
            });
          });
      },
      //关闭表单弹窗
      closeForm() {
        this.formVisible = false;
      },
      //完成添加刷新列表
      handleCancle() {
        this.formVisible = false;
        this.selectForm.page = 1;
        this.currentSelectedData = [];
        this.formData = {}
        this.$refs.dataTable.$refs.dataTable.clearSelection()
        this.getTableData();
      }
    },
  };
</script>
<style lang="scss" scoped>
  .index-container {
    box-sizing: border-box;
    width: 100%;
    padding: 0 30px !important;
    margin: 0 !important;
    background: #fff !important;

    .main-header {
      padding-top: 4px;
      margin-bottom: 24px;

      .search {
        width: 100%;

        .search-title {
          flex: 0 0 48px;
          width: 48px;
          font-size: 16px;
          line-height: 36px;
          color: #333;
        }
      }
    }
  }
</style>
